package bavaria.hightech.banking.bank;

import java.util.Iterator;

public class List<T> {
	private ListElement<T> head = new ListElement<T>(null, null);

	public void add(T elm) {
		ListElement<T> element = new ListElement<T>(elm, null);
		if (head.next == null) {
			head.next = element;
		} else {
			ListElement<T> currentElement = head.next;
			while (currentElement.next != null) {
				currentElement = currentElement.next;
			}
			currentElement.next = element;
		}
	}

	public Iterator<T> iterator() {
		return new ListIterator<T>(head);
	}

	public void clear() {
		head.next = null;
	}

	private class ListElement<E> {
		E data;
		ListElement<E> next;

		ListElement(E data, ListElement<E> next) {
			this.data = data;
			this.next = next;
		}
	}

	// innere klasse zum durchlaufen
	private class ListIterator<E> implements Iterator<E> {

		private ListElement<E> current;

		public ListIterator(ListElement<E> first) {
			this.current = first;

		}

		// durchlauf
		@Override
		public boolean hasNext() {
			return current.next != null;
		}

		@Override
		public E next() {
			if (current.next == null) {
				return null;
			}
			current = current.next;
			return current.data;
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException();
		}

	}
}
